package org.smog.core.config.security;

import org.smog.core.config.security.model.IAuthUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import java.util.Arrays;
import java.util.List;

/**
 * @Description: Created by IntelliJ IDEA.
 * @project_name: smogTemp
 * @time: 2020-05-15 14:31
 * @email: 17685306043@163.com
 * @author: huangZhongYao
 */
public class SecurityUtils {

	private SecurityUtils() {
		throw new UnsupportedOperationException("这是一个实用程序类，无法实例化!");
	}

	public static Authentication getAuthentication() {
		return SecurityContextHolder.getContext().getAuthentication();
	}

	public static IAuthUser getUser(Authentication authentication) {
		if (null != authentication) {
			// jdk 14才支持模式匹配
			if (authentication.getPrincipal() instanceof IAuthUser) {
				return (IAuthUser) authentication.getPrincipal();
			}
		}
		return null;
	}

	public static IAuthUser getUser() {
		return getUser(getAuthentication());
	}

	public static List<String> getRoles() {
		return Arrays.asList(getUser().getRoles());
	}

	public static List<String> getPermissions() {
		return Arrays.asList(getUser().getPermissions());
	}
}
